package com.student_exam_system.service.impl;


import com.student_exam_system.Util.MybatisUtils;
import com.student_exam_system.dao.ScoreMapper;
import com.student_exam_system.pojo.Score;
import com.student_exam_system.service.ScoreService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @author 86134
 */
@Service("scoreService")
public class ScoreServiceImpl implements ScoreService {
    private final SqlSession session = MybatisUtils.getSession();
    private final ScoreMapper scoreMapper = session.getMapper(ScoreMapper.class);


    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public List<Score> showScore(int choice, int classId, int paperId){
        List<Score> scores =null;
        try {
            if (choice == 1) {
                //单个班级的学生成绩情况
                scores = scoreMapper.studentScore(classId, paperId);
            }else if (choice == 2){
                //参加了这次考试的班级id
                List<Integer> classIds = scoreMapper.partOrNotIds(paperId);
                for (Integer id : classIds) {
                    //多个班级的平均分
                    scores = scoreMapper.classScore(paperId, id);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }finally {
            MybatisUtils.closeSession(session);
        }
        return scores;
    }
}
